﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using Maticsoft.DBUtility;//Please add references
namespace Travel.DAL
{
	/// <summary>
	/// 数据访问类:N_News
	/// </summary>
	public partial class N_News
	{
		public N_News()
		{}
		#region  Method

		/// <summary>
		/// 得到最大ID
		/// </summary>
		public int GetMaxId()
		{
		return DbHelperSQL.GetMaxID("ID", "N_News"); 
		}

		/// <summary>
		/// 是否存在该记录
		/// </summary>
		public bool Exists(int ID)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) from N_News");
			strSql.Append(" where ID=@ID");
			SqlParameter[] parameters = {
					new SqlParameter("@ID", SqlDbType.Int,4)
			};
			parameters[0].Value = ID;

			return DbHelperSQL.Exists(strSql.ToString(),parameters);
		}


		/// <summary>
		/// 增加一条数据
		/// </summary>
		public int Add(Travel.Model.N_News model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("insert into N_News(");
			strSql.Append("N_Classid,N_Title,N_Content,N_Time,N_Admin,N_Istop,N_Img,N_Focus,N_Count)");
			strSql.Append(" values (");
			strSql.Append("@N_Classid,@N_Title,@N_Content,@N_Time,@N_Admin,@N_Istop,@N_Img,@N_Focus,@N_Count)");
			strSql.Append(";select @@IDENTITY");
			SqlParameter[] parameters = {
					new SqlParameter("@N_Classid", SqlDbType.Int,4),
					new SqlParameter("@N_Title", SqlDbType.NVarChar,100),
					new SqlParameter("@N_Content", SqlDbType.NVarChar),
					new SqlParameter("@N_Time", SqlDbType.DateTime),
					new SqlParameter("@N_Admin", SqlDbType.NVarChar,50),
					new SqlParameter("@N_Istop", SqlDbType.Int,4),
					new SqlParameter("@N_Img", SqlDbType.NVarChar,50),
					new SqlParameter("@N_Focus", SqlDbType.Int,4),
					new SqlParameter("@N_Count", SqlDbType.Int,4)};
			parameters[0].Value = model.N_Classid;
			parameters[1].Value = model.N_Title;
			parameters[2].Value = model.N_Content;
			parameters[3].Value = model.N_Time;
			parameters[4].Value = model.N_Admin;
			parameters[5].Value = model.N_Istop;
			parameters[6].Value = model.N_Img;
			parameters[7].Value = model.N_Focus;
			parameters[8].Value = model.N_Count;

			object obj = DbHelperSQL.GetSingle(strSql.ToString(),parameters);
			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToInt32(obj);
			}
		}
		/// <summary>
		/// 更新一条数据
		/// </summary>
		public bool Update(Travel.Model.N_News model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("update N_News set ");
			strSql.Append("N_Classid=@N_Classid,");
			strSql.Append("N_Title=@N_Title,");
			strSql.Append("N_Content=@N_Content,");
			strSql.Append("N_Time=@N_Time,");
			strSql.Append("N_Admin=@N_Admin,");
			strSql.Append("N_Istop=@N_Istop,");
			strSql.Append("N_Img=@N_Img,");
			strSql.Append("N_Focus=@N_Focus,");
			strSql.Append("N_Count=@N_Count");
			strSql.Append(" where ID=@ID");
			SqlParameter[] parameters = {
					new SqlParameter("@N_Classid", SqlDbType.Int,4),
					new SqlParameter("@N_Title", SqlDbType.NVarChar,100),
					new SqlParameter("@N_Content", SqlDbType.NVarChar),
					new SqlParameter("@N_Time", SqlDbType.DateTime),
					new SqlParameter("@N_Admin", SqlDbType.NVarChar,50),
					new SqlParameter("@N_Istop", SqlDbType.Int,4),
					new SqlParameter("@N_Img", SqlDbType.NVarChar,50),
					new SqlParameter("@N_Focus", SqlDbType.Int,4),
					new SqlParameter("@N_Count", SqlDbType.Int,4),
					new SqlParameter("@ID", SqlDbType.Int,4)};
			parameters[0].Value = model.N_Classid;
			parameters[1].Value = model.N_Title;
			parameters[2].Value = model.N_Content;
			parameters[3].Value = model.N_Time;
			parameters[4].Value = model.N_Admin;
			parameters[5].Value = model.N_Istop;
			parameters[6].Value = model.N_Img;
			parameters[7].Value = model.N_Focus;
			parameters[8].Value = model.N_Count;
			parameters[9].Value = model.ID;

			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}

		/// <summary>
		/// 删除一条数据
		/// </summary>
		public bool Delete(int ID)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from N_News ");
			strSql.Append(" where ID=@ID");
			SqlParameter[] parameters = {
					new SqlParameter("@ID", SqlDbType.Int,4)
			};
			parameters[0].Value = ID;

			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		/// <summary>
		/// 批量删除数据
		/// </summary>
		public bool DeleteList(string IDlist )
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from N_News ");
			strSql.Append(" where ID in ("+IDlist + ")  ");
			int rows=DbHelperSQL.ExecuteSql(strSql.ToString());
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public Travel.Model.N_News GetModel(int ID)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select  top 1 ID,N_Classid,N_Title,N_Content,N_Time,N_Admin,N_Istop,N_Img,N_Focus,N_Count from N_News ");
			strSql.Append(" where ID=@ID");
			SqlParameter[] parameters = {
					new SqlParameter("@ID", SqlDbType.Int,4)
			};
			parameters[0].Value = ID;

			Travel.Model.N_News model=new Travel.Model.N_News();
			DataSet ds=DbHelperSQL.Query(strSql.ToString(),parameters);
			if(ds.Tables[0].Rows.Count>0)
			{
				if(ds.Tables[0].Rows[0]["ID"]!=null && ds.Tables[0].Rows[0]["ID"].ToString()!="")
				{
					model.ID=int.Parse(ds.Tables[0].Rows[0]["ID"].ToString());
				}
				if(ds.Tables[0].Rows[0]["N_Classid"]!=null && ds.Tables[0].Rows[0]["N_Classid"].ToString()!="")
				{
					model.N_Classid=int.Parse(ds.Tables[0].Rows[0]["N_Classid"].ToString());
				}
				if(ds.Tables[0].Rows[0]["N_Title"]!=null && ds.Tables[0].Rows[0]["N_Title"].ToString()!="")
				{
					model.N_Title=ds.Tables[0].Rows[0]["N_Title"].ToString();
				}
				if(ds.Tables[0].Rows[0]["N_Content"]!=null && ds.Tables[0].Rows[0]["N_Content"].ToString()!="")
				{
					model.N_Content=ds.Tables[0].Rows[0]["N_Content"].ToString();
				}
				if(ds.Tables[0].Rows[0]["N_Time"]!=null && ds.Tables[0].Rows[0]["N_Time"].ToString()!="")
				{
					model.N_Time=DateTime.Parse(ds.Tables[0].Rows[0]["N_Time"].ToString());
				}
				if(ds.Tables[0].Rows[0]["N_Admin"]!=null && ds.Tables[0].Rows[0]["N_Admin"].ToString()!="")
				{
					model.N_Admin=ds.Tables[0].Rows[0]["N_Admin"].ToString();
				}
				if(ds.Tables[0].Rows[0]["N_Istop"]!=null && ds.Tables[0].Rows[0]["N_Istop"].ToString()!="")
				{
					model.N_Istop=int.Parse(ds.Tables[0].Rows[0]["N_Istop"].ToString());
				}
				if(ds.Tables[0].Rows[0]["N_Img"]!=null && ds.Tables[0].Rows[0]["N_Img"].ToString()!="")
				{
					model.N_Img=ds.Tables[0].Rows[0]["N_Img"].ToString();
				}
				if(ds.Tables[0].Rows[0]["N_Focus"]!=null && ds.Tables[0].Rows[0]["N_Focus"].ToString()!="")
				{
					model.N_Focus=int.Parse(ds.Tables[0].Rows[0]["N_Focus"].ToString());
				}
				if(ds.Tables[0].Rows[0]["N_Count"]!=null && ds.Tables[0].Rows[0]["N_Count"].ToString()!="")
				{
					model.N_Count=int.Parse(ds.Tables[0].Rows[0]["N_Count"].ToString());
				}
				return model;
			}
			else
			{
				return null;
			}
		}

		/// <summary>
		/// 获得数据列表
		/// </summary>
		public DataSet GetList(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select ID,N_Classid,N_Title,N_Content,N_Time,N_Admin,N_Istop,N_Img,N_Focus,N_Count ");
			strSql.Append(" FROM N_News ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			return DbHelperSQL.Query(strSql.ToString());
		}

		/// <summary>
		/// 获得前几行数据
		/// </summary>
		public DataSet GetList(int Top,string strWhere,string filedOrder)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select ");
			if(Top>0)
			{
				strSql.Append(" top "+Top.ToString());
			}
			strSql.Append(" ID,N_Classid,N_Title,N_Content,N_Time,N_Admin,N_Istop,N_Img,N_Focus,N_Count ");
			strSql.Append(" FROM N_News ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			strSql.Append(" order by " + filedOrder);
			return DbHelperSQL.Query(strSql.ToString());
		}

		/// <summary>
		/// 获取记录总数
		/// </summary>
		public int GetRecordCount(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) FROM N_News ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			object obj = DbHelperSQL.GetSingle(strSql.ToString());
			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToInt32(obj);
			}
		}
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("SELECT * FROM ( ");
			strSql.Append(" SELECT ROW_NUMBER() OVER (");
			if (!string.IsNullOrEmpty(orderby.Trim()))
			{
				strSql.Append("order by T." + orderby );
			}
			else
			{
				strSql.Append("order by T.ID desc");
			}
			strSql.Append(")AS Row, T.*  from N_News T ");
			if (!string.IsNullOrEmpty(strWhere.Trim()))
			{
				strSql.Append(" WHERE " + strWhere);
			}
			strSql.Append(" ) TT");
			strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
			return DbHelperSQL.Query(strSql.ToString());
		}

		/*
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetList(int PageSize,int PageIndex,string strWhere)
		{
			SqlParameter[] parameters = {
					new SqlParameter("@tblName", SqlDbType.VarChar, 255),
					new SqlParameter("@fldName", SqlDbType.VarChar, 255),
					new SqlParameter("@PageSize", SqlDbType.Int),
					new SqlParameter("@PageIndex", SqlDbType.Int),
					new SqlParameter("@IsReCount", SqlDbType.Bit),
					new SqlParameter("@OrderType", SqlDbType.Bit),
					new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
					};
			parameters[0].Value = "N_News";
			parameters[1].Value = "ID";
			parameters[2].Value = PageSize;
			parameters[3].Value = PageIndex;
			parameters[4].Value = 0;
			parameters[5].Value = 0;
			parameters[6].Value = strWhere;	
			return DbHelperSQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
		}*/

		#endregion  Method
	}
}

